package cn.edu.scau.cmi.liangzaoqing.controller.page;

import cn.edu.scau.cmi.liangzaoqing.domain.UserTest;
import cn.edu.scau.cmi.liangzaoqing.repository.UserTestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserTestController {
    @Autowired
    UserTestRepository userTestRepository;

    @RequestMapping(value ="/newUserTest")
    public String newUser(Model model, UserTest userTest) {
        model.addAttribute("userTest", userTest);
        UserTest isNewUserTest = userTestRepository.findUserByName(userTest.getName());
        //判断该用户名是否被注册过
        if (isNewUserTest == null) {
            userTestRepository.save(userTest);
            return "success";
        } else {
            return "false";
        }
    }

    @RequestMapping(value ="/listUserTest")
    public ModelAndView listUsers() {
        ModelAndView modelAndView = new ModelAndView();
        UserTest user = userTestRepository.findUserByName("zhangsan");
        System.out.println(user.getName() + user.getId());
        modelAndView.addObject("user", user);
        modelAndView.setViewName("suecess");
        return modelAndView;

    }



}
